<template>
  <div class="role">
    <page-search :searchFormConfig="searchFormConfig"></page-search>
    <div class="bottom-box">
      <page-content
        :contentTableConfig="contentTableConfig"
        pageName="role"
        @newBtnClick="handleNewData"
        @editBtnClick="handleEditData"
      >
        <template #leftSlot>角色列表</template>
        <template #rightSlot>新建角色</template>
      </page-content>
    </div>
    <pageModal
      :defaultInfo="defaultInfo"
      :otherInfo="otherInfo"
      :modalConfig="modalConfig"
      ref="pageModalRef"
      pageName="role"
    >
      <div class="tree">
        <el-tree
          ref="elTreeRef"
          :data="menus"
          show-checkbox
          node-key="id"
          :props="{ children: 'children', label: 'name' }"
          @check="handleCheckChange"
        />
      </div>
    </pageModal>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed, ref, nextTick } from "vue";
import { useStore } from "@/store";
import { menuMapLeafKeys } from "@/utils/map-menus";

import PageSearch from "@/components/page-search";
import PageContent from "@/components/page-content";
import { contentTableConfig } from "./config/content.config";
import { searchFormConfig } from "./config/search.config";
import { modalConfig } from "./config/modal.config";
import { usePageModal } from "@/hooks/use-page-modal";

import { ElTree } from "element-plus";
export default defineComponent({
  name: "role",
  components: {
    PageSearch,
    PageContent,
  },
  setup() {
    const elTreeRef = ref<InstanceType<typeof ElTree>>();
    const editCallbacks = (item: any) => {
      const leafKeys = menuMapLeafKeys(item.menuList);
      nextTick(() => {
        elTreeRef.value?.setCheckedKeys(leafKeys, false);
      });
    };
    const [pageModalRef, defaultInfo, handleEditData, handleNewData] =
      usePageModal(undefined, editCallbacks);

    const store = useStore();
    const menus = computed(() => store.state.entireMenu);

    const otherInfo = ref({});

    const handleCheckChange = (data1: any, data2: any) => {
      // 选框改变是触发
      const checkedKeys = data2.checkedKeys;
      const halfCheckedKeys = data2.halfCheckedKeys;
      const menuList = [...checkedKeys, ...halfCheckedKeys];
      // 下面这条语法不太懂
      //与下面同效 otherInfo.value = { menuList：menuList };
      otherInfo.value = { menuList };
      console.log(otherInfo.value);
    };
    return {
      searchFormConfig,
      contentTableConfig,
      modalConfig,
      pageModalRef,
      defaultInfo,
      handleEditData,
      handleNewData,
      menus,
      handleCheckChange,
      otherInfo,
      elTreeRef,
    };
  },
});
</script>

<style scoped lang="less">
.bottom-box {
  margin-top: 20px;
  padding: 20px;
  background-color: #ffffff;
}
.tree {
  margin-left: 60px;
}
</style>
